package com.shi.controller;

import com.shi.entity.ShiVO;
import com.shi.es.ShiRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.shi.service.ShiService;
import com.shi.entity.Shi;

import java.util.Optional;

/**
 * Shi的路由接口服务
 * 
 * @author 
 *
 */
@Slf4j
@RestController
@RequestMapping("/poetry")
public class ShiController {

	@Autowired
	ShiRepository shiRepository;

	/** ShiService服务 */
	@Autowired
	private ShiService shiService;
	
	/**
	 * 查询所有Shi数据的方法
	 * @param value
	 * @return
	 */
	@GetMapping(value = "/list", produces = {"application/json;charset=UTF-8"})
	public String find(ShiVO value) {
		//System.out.println("列表");
		//log.info("invoke\n");
		String val ="" ;
		try{
			val = shiService.find(value);
		}catch (Exception e){
			log.error(e.toString());
			e.printStackTrace();
		}
		return val;
	}

	@GetMapping(value = "/search", produces = {"application/json;charset=UTF-8"})
	public String serach(ShiVO value) {

		return shiService.search(value);
	}
	
	/**
	 * 通过id查询Shi数据的方法
	 * @param id
	 * @return
	 */
	@GetMapping(value = "/{id}", produces = {"application/json;charset=UTF-8"})
	public String findOne(@PathVariable(name="id") Integer id) {
		return shiService.findOne(id);
	}
	
	/**
	 * 插入Shi属性不为空的数据方法
	 * @param id
	 * @return
	 */
	/*@PostMapping(value = "/save", produces = {"application/json;charset=UTF-8"})
	public String save(Shi value) {
		return shiService.saveNotNull(value);
	}*/
	
	/**
	 * 更新Shi属性不为空的数据方法
	 * @param id
	 * @return
	 */
	@PutMapping(value = "/update", produces = {"application/json;charset=UTF-8"})
	public String update(Shi value) {
		return shiService.updateNotNullById(value);
	}
	
	/**
	 * 通过id删除Shi数据方法
	 * @param id
	 * @return
	 */
	@DeleteMapping(value = "/delete/{id}", produces = {"application/json;charset=UTF-8"})
	public String delete(@PathVariable(name="id") Integer id) {
		return shiService.deleteById(id);
	}

	@RequestMapping("saveToEs")
	public Shi saveToEs(){
		Shi shi = new Shi();
		shi.setAuthor("秦岭");
		shi.setContent("好雨知时节，当春乃发生。随风潜入夜，润物细无声。野径云俱黑，江船火独明。晓看红湿处，花重锦官城。");
		shi.setDynasty("现代");
		shi.setYunlvRule("五言绝句");
		shi.setId(99999999);
		shi.setTitle("好诗");
		Shi res = shiRepository.save(shi);
		return res;
	}

	@RequestMapping("queryFromEs")
	public Shi queryFromEs(){

		Optional<Shi> res = shiRepository.findById(888);
		if(res.isPresent()){
			return res.get();
		}else {
			return new Shi();
		}
	}
}
